#include "GameLogic.h"

#define UI_INTERVAL 150 //interval in milliseconds for timing user input (time to wait until next user input is handled)

GameLogic::GameLogic(void)
{
}

GameLogic::~GameLogic(void)
{
}

void GameLogic::init(CL_GraphicContext& gc)
{	
	CL_FontDescription font_desc;
	font_desc.set_typeface_name("tahoma");
	font_desc.set_height(12);
	font_desc.set_anti_alias(false);
	smallFont = CL_Font_System(gc, font_desc);
	
	font_desc.set_height(72);
	largeFont = CL_Font_System(gc, font_desc);	
	
	floor = 550;

	platform1 = 400;
	platform2 = 460; //platform to land on

	maxCollisionSpeed = 5.0f;
	displayScoreTiming = 1000;

	debugOn = true;

	reset();
}

void GameLogic::mouseDown(const CL_InputEvent &event, const CL_InputState &state)
{
}

void GameLogic::keyDown(const CL_InputEvent &event, const CL_InputState &state)
{
	if(gameState != RUN) {
		if(timer > 0)
			return;

		reset();
	}

	if(event.id == CL_KEY_UP) {
		lander.setVerticalThrust(lander.getMaxThrust());
	} else if(event.id == CL_KEY_LEFT) {
		lander.setHorizontalThrust(-lander.getMaxThrust()/2);
	} else if(event.id == CL_KEY_RIGHT) {
		lander.setHorizontalThrust(lander.getMaxThrust()/2);
	}
}

void GameLogic::keyUp(const CL_InputEvent &event, const CL_InputState &state)
{
	if(event.id == CL_KEY_UP) {
		lander.setVerticalThrust(0);
	} else if(event.id == CL_KEY_LEFT || event.id == CL_KEY_RIGHT) {
		lander.setHorizontalThrust(0);
	}
}

void GameLogic::update(int delta)
{
	if(gameState != RUN) {
		timer-=delta;
		return;
	}
	
	lander.update(delta);

	collisions();
}

void GameLogic::collisions()
{
	if(lander.getPosition().y >= floor ) //collision!
	{
		if(lander.getVelocity().length() > maxCollisionSpeed ||
			lander.getPosition().x < platform1 ||
			lander.getPosition().x > platform2)
			gameState = CRASHED;
		else
			gameState = LANDED;
		
		timer=displayScoreTiming;
	}
}


void GameLogic::draw(CL_GraphicContext& gc)
{	
	//draw floor
	drawWorld(gc);
	
	//draw lander
	lander.draw(gc);
	
	//draw fuel gauge
	CL_Point pos(750, 10);
	float h = 200.0f;
	float w = 10.0f;
	CL_Colorf barColor = CL_ColorHSVf(120*lander.getFuelPercent(),1,1,1);
	CL_Draw::fill(gc, pos.x, pos.y+h, pos.x+w, pos.y+h-h*lander.getFuelPercent(), barColor);
	CL_Draw::box(gc, pos.x, pos.y, pos.x+w, pos.y+h, CL_Colorf::white);


	//draw text
	if(gameState==LANDED) {
		largeFont.draw_text(gc, 100,400,"The eagle has landed!", CL_Colorf::green);
	} else if(gameState==CRASHED) {
		largeFont.draw_text(gc, 200,400,"You fail!", CL_Colorf::red);
	}

	if(debugOn)
	{
		//debug text
		smallFont.draw_text(gc, 10,10,cl_format("Altitude: %1",floor-lander.getPosition().y));
		smallFont.draw_text(gc, 10,22,cl_format("Speed: %1", lander.getVelocity().length() ));
		smallFont.draw_text(gc, 10,34,cl_format("Fuel: %1", lander.getFuel() ));
	}
}

void GameLogic::drawWorld(CL_GraphicContext& gc)
{	
	CL_Pointf bl(0,floor);
	CL_Pointf br(800,floor);

	//draw floor
	CL_Draw::line(gc,bl,br, CL_Colorf::white);

	//draw platform
	CL_Draw::line(gc, platform1, floor, platform2, floor, CL_Colorf::red);
}

void GameLogic::reset()
{
	//timeSinceUserInput=0;
	lander.reset();
	gameState = RUN;
}